package com.cssw.fyzb.tps.config.pay;

import com.alipay.api.AlipayApiException;
import com.ijpay.alipay.AliPayApiConfig;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author: chenchao
 */
@Component
@Setter
@Getter
public class AliPay {

    @Value("${config.ali.value.appId}")
    private String appId;

    @Value("${config.ali.value.privateKey}")
    private String privateKey;

    @Value("${config.ali.value.publicKey}")
    private String publicKey;

    @Value("${config.ali.value.appCertPath}")
    private String appCertPath;

    @Value("${config.ali.value.aliPayCertPath}")
    private String aliPayCertPath;

    @Value("${config.ali.value.aliPayRootCertPath}")
    private String aliPayRootCertPath;

    @Value("${config.ali.value.serviceUrl}")
    private String serviceUrl;

    @Value("${config.ali.value.domain}")
    private String domain;

    @Value("${config.ali.value.signType}")
    private String signType;

    private AliPayApiConfig apiConfig;

    private static final Logger logger = LoggerFactory.getLogger(AliPay.class);

    public AliPayApiConfig getApiConfig() {
        if (apiConfig == null) {
            synchronized (AliPay.class) {
                if (apiConfig == null) {
                    try {
                        apiConfig = AliPayApiConfig.builder()
                                .setAppId(appId)
                                .setAliPayPublicKey(publicKey)
                                .setAppCertPath(appCertPath)
                                .setAliPayCertPath(aliPayCertPath)
                                .setAliPayRootCertPath(aliPayRootCertPath)
                                .setCharset("utf-8")
                                .setPrivateKey(privateKey)
                                .setServiceUrl(serviceUrl)
                                .setSignType(signType)
                                .buildByCert();
                    } catch (AlipayApiException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return apiConfig;
    }
}
